<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools :show-before="false">
        <template v-slot:right>
          <el-button type="primary" size="small" @click="addFn('0',1)">添加权限</el-button>
        </template>
      </PageTools>
      <el-table row-key="id" :data="permissionList" border>
        <el-table-column prop="name" label="名称" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.pid==='0'" type="text" @click="addFn(row.id,2)">添加</el-button>
            <el-button type="text" @click="editFn(row.id)">编辑</el-button>
            <el-button type="text" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <AddPerm
        ref="addPerm"
        :show-dialog.sync="showDialog"
        @updateEvent="getPermissionList()"
      />
    </div>
  </div>
</template>

<script>
import { delPermission, getPermissions } from '@/api/permission'
import { listToTree } from '@/utils'
import AddPerm from './components/add-permission.vue'
export default {
  components: {
    AddPerm
  },
  data() {
    return {
      permissionList: [],
      showDialog: false
    }
  },
  created() {
    this.getPermissionList()
  }, methods: {
    editFn(id) {
      this.showDialog = true
      this.$refs.addPerm.getPermissionlDetail(id)
    },
    async getPermissionList() {
      const res = await getPermissions()
      this.permissionList = listToTree(res, '0')
    },
    addFn(pid, type) {
      this.showDialog = true
      this.$refs.addPerm.formData.pid = pid
      this.$refs.addPerm.formData.type = type
    },
    delFn(id) {
      this.$confirm('是否确定删除权限', '删除', {
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.getPermissionList()
      }).catch(() => {
        console.log('点击了取消按钮')
      })
    }
  }
}
</script>

  <style>

  </style>
